package tcu.iot.gasmanage.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

public class HttpUtils {
    public static JSONObject sendPostRequest(String txt) {
        //定义RESTful连接器地址
        String url = "http://124.70.14.87:6041/rest/sql";
        RestTemplate client = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();

        //将包含用户名root，密码taosdata，的Authorization信息加入POST请求头
        httpHeaders.add("Authorization", "Basic cm9vdDp0YW9zZGF0YQ==");
        //设置编码信息
        httpHeaders.setContentType(MediaType.TEXT_PLAIN);
        //将HTTP请求，请求体和请求头封装为一个实体
        HttpEntity<Map<String, String>> requestEntity = new HttpEntity(txt, httpHeaders);
        //发送Http POST请求
        ResponseEntity<JSONObject> response = client.postForEntity(url, requestEntity, JSONObject.class);

        return response.getBody();
    }
}
